<?php


namespace app\admin\controller;


use app\common\controller\Common;
use think\Controller;
use think\Db;
use think\Exception;
use think\Request;
use think\Session;

class File extends Controller
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        //获取平台标题
        $platform = Db::name("config_platform")->find();
        $this->assign("platform", $platform);
        $swidthStyle = Session::get("style");
        $this->assign("swidthStyle",$swidthStyle);
    }
    function getFilesize($num) {
        $p = 0;
        $format = 'bytes';
        if( $num > 0 && $num < 1024 ) {
            $p = 0;
            return number_format($num) . ' ' . $format;
        }
        if( $num >= 1024 && $num < pow(1024, 2) ){
            $p = 1;
            $format = 'KB';
        }
        if ( $num >= pow(1024, 2) && $num < pow(1024, 3) ) {
            $p = 2;
            $format = 'MB';
        }
        if ( $num >= pow(1024, 3) && $num < pow(1024, 4) ) {
            $p = 3;
            $format = 'GB';
        }
        if ( $num >= pow(1024, 4) && $num < pow(1024, 5) ) {
            $p = 3;
            $format = 'TB';
        }
        $num /= pow(1024, $p);
        return number_format($num, 2) . ' ' . $format;
    }


    public function index(){
        //权限查询
        $common = new Common();
        $powerList = $common->showpower();
        $this->assign("powerList",$powerList);

        $table = Session::get("table");
        $thisWechat = Session::get("thisWechat");
        $where['uniacid'] = $thisWechat['id'];
        if($table!="account"){
            $where['table'] = $table;
            $where['add_id'] = Session::get("id");
        }
        $order = "id desc";
        $sort = '';
        if(!empty($_GET['sort'])){
            $sort = $_GET['sort'];
            $order =  $_GET['sort']." desc";
        }

        $type = 0;
        if(!empty($_GET['type'])){
            $type = $_GET['type'];
            $where['type'] = $type;
        }
        if(!empty($_GET['show'])) {
            try{
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;

                $list = Db::name("upload_images")->where($where)->limit($start,$limit)->order($order)->select();
                $role = [
                    'account'=>"平台管理员",
                    'admin_agent'=>"代理商",
                    'admin_colony_manager'=>"社区管理员",
                    'admin_manager'=>"小区管理员",
                    'admin_gridmember'=>"网格员",
                    'stores_manage'=>"商户",
                    'admin_fans'=>"平台用户",
                ];
                foreach ($list as $k=>$v){
                    $list[$k]['size'] = $this->getFilesize($v['size']);
                    //查询上传人信息
                    if(!empty($v['table'])){
                        $table = $v['table'];
                        $user = Db::name($v['table'])->where(['id'=>$v['add_id']])->find();
                        if($table == "account"){
                            $list[$k]['add_id'] = $user['account'];
                        }elseif ($table=="admin_fans"){
                            $list[$k]['add_id'] = $user['nickname'];
                        }else{
                            $list[$k]['add_id'] = $user['name'];
                        }
                    }
                    $list[$k]['table'] = empty($v['table'])?'':$role[$v['table']];
                    $list[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
                    $list[$k]['name'] = '<a href="'.$v['path'].'" target="_blank">'.$v['name'].'</a>';
                }

                $count = Db::name("upload_images")->where($where)->count();
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            }catch (Exception $e){
                $black['code'] = 202;
                $black['data'] = $e->getMessage();
            }
            return $black;
        }



        $assign = [
            "sort"=>$sort,
            "type"=>$type
        ];
        $this->assign($assign);
        return $this->fetch('index');
    }

    //删除文件
    public function delFile(){
        try{
            if(empty($_POST['id'])){
                throw new Exception("请选择要删除的数据");
            }
            $id = $_POST['id'];
            foreach ($id as $k=>$v){
                $this->deleteObject($v);
            }
            $black['code'] = 0;
            $black['msg'] = "删除成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


    //腾讯云账号信息
    public function objectConfig(){
        require ROOT_PATH."extend/cos/vendor/autoload.php";//引入PHPExcel加载文件
        $secretId = "AKIDDAqOnvZfYeZDbrjYzFMD4ezhhXiYKUYK"; //"云 API 密钥 SecretId";
        $secretKey = "zi9JM6jG6gz3vb3E9tR4RbW9bnCOKzRf"; //"云 API 密钥 SecretKey";
        $region = "ap-nanjing"; //设置一个默认的存储桶地域

        $cosClient = new \Qcloud\Cos\Client(
            array(
                'region' => $region,
                'schema' => 'http', //协议头部，默认为http
                'credentials'=> array(
                    'secretId'  => $secretId ,
                    'secretKey' => $secretKey)));
        return $cosClient;
    }

    //腾讯云删除
    public function deleteObject($id){
        $thisWechat = Session::get("thisWechat");
        $cosClient = $this->objectConfig();
        $detail = Db::name("upload_images")->where(['id'=>$id])->find();
        try {
            $key = 'platform'.$detail['uniacid'].'/'.$detail['insertdata'].'/'.$detail['name'];
            $result = $cosClient->deleteObject(array(
                'Bucket' => 'community-1253901423', //格式：BucketName-APPID
                'Key' => $key,
            ));
            Db::name("upload_images")->where(['id'=>$id])->delete();
        } catch (\Exception $e) {
            // 请求失败
            echo($e);
        }
    }

}